package designPattern.observer;

import java.util.ArrayList;

/**
 * 类是核心
 * 1,包含最新的天气情况信息
 * 2,合有观察者集合，使用ArrayList管理
 * 3,当数据有更新时，就主动的调用ArrayList,通知他们（接入方）就看到最新的信息
 */
public class WeatherData implements Subject{
    //温度、气压、湿度
    private float temperature;
    private float pressure;
    private float humidity;

    //观察者集合
    private ArrayList<Observer> observers;


    public WeatherData() {
        this.observers = new ArrayList<>();

    }

    public float getTemperature() {
        return temperature;
    }

    public float getPressure() {
        return pressure;
    }

    public float getHumidity() {
        return humidity;
    }


    public void dataChange(){
        notifyObserver();
    }

    public void setData(float temperature,float pressure,float humidity){
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        dataChange();
    }

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        if(observers.contains(o)) {
            observers.remove(o);
        }
    }


    //遍历所有观察者并通知
    @Override
    public void notifyObserver() {
        for (Observer observer : observers) {
            observer.update(temperature,pressure,humidity);

        }
    }
}
